/*
 * @Description:角色controller
 * @Version: 2.0
 * @Autor: kafei
 * @Date: 2021-11-19 14:45:19
 * @LastEditors: kafei
 * @LastEditTime: 2021-12-20 16:54:28
 */
// const { getJWTPayload } = require('../config/key')
const result = require('../utils/result')
const { PageModel } = require('../model/pageModel')
const { filterObj } = require('../utils/index.js')
const {
  roleServer,
  findRoleServer,
  updateRoleServer
} = require('../server/roleServer')
function compare(property) {
  return function(a, b) {
    var value1 = a[property]
    var value2 = b[property]
    return value1 - value2
  }
}
async function updateRoleController(res) {
  const keys = [
    'rName',
    'rdesc',
    'rStatus',
    'userId',
    'mid',
    'id',
    'rid'
  ]
  const target = filterObj(res, keys)
  const data = await updateRoleServer(target)
  return result('0', { data })
}

async function roleController(role) {
  const data = await roleServer(role)
  const orderData = data.sort(compare('orderByMenu'))
  return result('0', { data: orderData })
}
async function findRoleServerController() {
  const data = await findRoleServer()
  return result('0', { data })
}

async function findPageRoleServerController(ctx) {
  const { page, size } = ctx.query
  const data = await findRoleServer(ctx.query)
  return result('0', { data: new PageModel(page, size, data) }) // 创建分页模型
}

module.exports = {
  roleController,
  findRoleServerController,
  findPageRoleServerController,
  updateRoleController
}
